/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.ControlLayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javaapplication6.DomainLayer.Customer;
import javaapplication6.DomainLayer.StatisticsLogOrders;
import javaapplication6.DomainLayer.OrderSellProduct;
import javaapplication6.DomainLayer.OrderSellRecords;
import javaapplication6.DomainLayer.SellProduct;
import javaapplication6.DomainLayer.StatisticsOfCompany;

/**
 *
 * @author meydlo
 */
public class OrderSellControl {

    private long orderId=0;

    private OrderSellRecords orderSellRecords=OrderSellRecords.getOrderSellRecords();
    private SellControl sellControl;
    private CustomerControl customerControl;

    public OrderSellControl()
    {
        sellControl=new SellControl();
        customerControl=new CustomerControl();
    }

    //define How many of one product put to Basket, returns products of one type cust want to buy (noOfOneType)
    public OrderSellProduct addProduct(long idOfCustomer,long idOfProduct, int noOfOneType)
    {
        //define sellproduct
        SellProduct sellProduct=sellControl.findSellProduct(idOfProduct);

        //create and set orderSellProduct
        OrderSellProduct orderSellProduct=setOrderSellProduct(sellProduct,noOfOneType,idOfCustomer);
            
        return orderSellProduct;
    }

    //int index= ordinal number of sellproduct in Arraylist<OrderSellProduct>
    public double getTotalPriceOfOneType(OrderSellProduct orderSellProduct,boolean isDiscount)
    {
        //declaring variables of orderSellProduct necessary to count prices
        double price=orderSellProduct.getPrice();
        double discount=orderSellProduct.getDiscount();
        int noOfOneType=orderSellProduct.getNoOfOneType();

        //sum:
        double totalPrice=price*noOfOneType;

        if(isDiscount==true){
            double totalPriceAfterDiscount=((totalPrice)*((100-discount)/100));
            return totalPriceAfterDiscount;
        }
        return totalPrice;
    }

  
    //return how much the customer has to pay for order
    public double getTotalPriceOfBasket(ArrayList<OrderSellProduct> orderSellProductList,boolean isAfterDiscount)
    {
        //declare total price of all products in basket
        double totalPrice=0;

        //counting (no discount)
        if(isAfterDiscount==false){
            for(int i=0;i<orderSellProductList.size();i++){
                OrderSellProduct orderSellProduct=orderSellProductList.get(i);
                totalPrice+=getTotalPriceOfOneType(orderSellProduct,false);
            }
        }
        //if we want to getTotal price of basket after discount
        else if(isAfterDiscount==true){
            for(int i=0;i<orderSellProductList.size();i++){
                OrderSellProduct orderSellProduct=orderSellProductList.get(i);
                totalPrice+=getTotalPriceOfOneType(orderSellProduct,true);
            }
        }
        return totalPrice;
    }

    public void printOrderSellProduct(OrderSellProduct orderSellProduct)
    {
        //set variables (name: noOfOneType: price: discout:)
        String name=orderSellProduct.getName();
        int noOfOneType=orderSellProduct.getNoOfOneType();
        double price=orderSellProduct.getPrice();
        double  discount=orderSellProduct.getDiscount();

        //set variables of one kind
        double moneyForOneKind=getTotalPriceOfOneType(orderSellProduct,false);
        double moneyForOneKindAfterDiscount=getTotalPriceOfOneType(orderSellProduct,true);

        System.out.println("Product name:"+name+
            " | amount: "+noOfOneType+
            " | price: "+price+
            " | discount"+discount+
            "\ntotal: "+moneyForOneKind+
            "\ntotal after discount: "+moneyForOneKindAfterDiscount
            );
    }
    public void printBasket(ArrayList<OrderSellProduct> orderSellProductList)
    {
        //declare here because loop
        double totalOfBasket=0;
        double totalOfBasketAfterDiscount=0;
        OrderSellProduct orderSellProduct=null;

        //iterate over whole basket
        for(int i=0;i<orderSellProductList.size(); i++){

            //declaring product from where we gain informations
            orderSellProduct=orderSellProductList.get(i);

            //set variables of basket
            totalOfBasket=getTotalPriceOfBasket(orderSellProductList, false);
            totalOfBasketAfterDiscount=getTotalPriceOfBasket(orderSellProductList,true);

        }
 
        //printing informations for One product;
            printOrderSellProduct(orderSellProduct);
        System.out.println("--------------------------"+
                "\nTotal price: "+totalOfBasket+
                "\nyou pay (after discount): "+totalOfBasketAfterDiscount+
                "\nyou save: "+(totalOfBasket-totalOfBasketAfterDiscount)+
                "\n############################################"
        );
        
    }


    //put to hashmap in (OrderRecords) (id of order)+(basket)
    public void storeOrderToHashMap(ArrayList<OrderSellProduct> orderSellBasketList)
    {
        orderId+=1;
        Long idLong=new Long(orderId);
        orderSellRecords.getOrderSellHashMap().put(idLong, orderSellBasketList);
    }

    //confirm or cancel ordering
    public void confirmOrder(ArrayList<OrderSellProduct> orderSellProductList, boolean isConfirmed)
    {
        if(isConfirmed==true){
            //set goods(ArrayList<OrderSellProduct>) to (HashMap<Long, ArrayList<OrderSellProduct> >)
            storeOrderToHashMap(orderSellProductList);

            System.out.println("Order was registered!");
            System.out.println("\n##### Your order ID: "+getOrderId()+" #####\n");
        }
        else{
            orderSellProductList.clear();
            System.out.println("Order was canceled");
        }
    }

    public void confirmToBuy(long orderId, boolean isBuy){

        //declare orders(hashmap)
        HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap=orderSellRecords.getOrderSellHashMap();
        //extract orderSellProductList from HashMap
        ArrayList<OrderSellProduct> orderSellProductList=orderSellHashMap.get(orderId);

        //if C want to buy -> order in (OrderSellHashMap) -> moving to -> log (logOrderSellHashMap)
        if(isBuy==true){
            //define LOgOrders
            StatisticsLogOrders logOrders=StatisticsLogOrders.getLogOrders();

            // define LogOrderHashMap
            HashMap<Long, ArrayList<OrderSellProduct>> logOrderSellHashMap=logOrders.getLogOrdersSellHashMap();

            //add basket to LogList
            logOrderSellHashMap.put(orderId, orderSellProductList);

            //putting to statistics
            double payment=getTotalPriceOfBasket(orderSellProductList, true);
            StatisticsOfCompany statisticsOfCompany=StatisticsOfCompany.getStatisticsOfCompany();
            statisticsOfCompany.setMoneyFromSell(payment);

            //remove order From OrderHashMap
            orderSellHashMap.remove(orderId);
        }
        //(return goods back to stok) && (remove order from HashMap)
        else{
            //return products to ArrayList<SellProduct>
            returnProductsToProductList(orderSellProductList);

            //remove order From OrderHashMap
            orderSellHashMap.remove(orderId);
        }
            
            
        
    }

    public void removeAllProductsfromProductList(ArrayList<OrderSellProduct> orderSellProductList)
    {
        for(int i=0; i<orderSellProductList.size(); i++){

            //declare particular orderSellProduct and SellProduct
            OrderSellProduct orderSellProduct=orderSellProductList.get(i);
            SellProduct sellProduct=sellControl.findSellProduct(i);

            //set id of product to remove
            long idOfProduct=orderSellProduct.getId();
            //how many to remove
            int numToRemove=orderSellProduct.getNoOfOneType();

            //remove (each num of product) of (each product which exist in ArrayList<OrderSellProduct>)
            removeProductFromProductList(idOfProduct, numToRemove);
        }


    }

    public void removeProductFromProductList(long idOfProduct,int numToRemove)
    {
        SellProduct sellProduct=sellControl.findSellProduct(idOfProduct);
        //how meny is in stok
        int noOfProduct=sellProduct.getNoOfOneType();

        //set rest goods in stok
        int rest=noOfProduct-numToRemove;


        //set value in SellProductRecords
        sellProduct.setNoOfOneType(rest);
    }

    public void addSellProductToSellProductList(long idOfProduct,int numToAdd, SellProduct sellProduct)
    {
         //how meny is in stok
        int noOfProduct=sellProduct.getNoOfOneType();

        //set rest goods in stok
        int goodsAfterAdded=noOfProduct+numToAdd;

        //set value in SellProductRecords
        sellProduct.setNoOfOneType(goodsAfterAdded);
    }


    public void returnProductsToProductList(ArrayList<OrderSellProduct> orderSellProductList)
    {
        for(int i=0; i<orderSellProductList.size(); i++){

            //declare particular orderSellProduct from where gain inf
            OrderSellProduct orderSellProduct=orderSellProductList.get(i);

            //id of product
            long idOfProduct=orderSellProduct.getId();

            SellProduct sellProduct=sellControl.findSellProduct(idOfProduct);

            //how many to add
            int numToAdd=orderSellProduct.getNoOfOneType();

            //how meny is in stok
            int noOfProduct=sellProduct.getNoOfOneType();

            //set rest goods in stok
            int goodsAfterAdded=noOfProduct+numToAdd;

            //set value in SellProductRecords
            sellProduct.setNoOfOneType(goodsAfterAdded);

        }
    }



    //declaring and returning (OrderSellProduct)
    public OrderSellProduct setOrderSellProduct(SellProduct sellProduct, int noOfOneType, long customerId)
    {
        //create object of OrderSellProduct
        OrderSellProduct orderSellProduct=new OrderSellProduct();

        //create instance variables (preparing for set to orderSellProduct)
        long productId=sellProduct.getId();
        String nameOfProduct=sellProduct.getName();
        String description=sellProduct.getDescription();
        double price=sellProduct.getPrice();
        double discount=getBiggerDiscount(customerId, productId);

        //set informations to orderSellProduct;
        orderSellProduct.setId(productId);
        orderSellProduct.setName(nameOfProduct);
        orderSellProduct.setPrice(price);
        orderSellProduct.setNoOfOneType(noOfOneType);
        orderSellProduct.setCustomerId(customerId);
        orderSellProduct.setDescription(description);
        orderSellProduct.setDiscount(discount);

        return orderSellProduct;
    }

    public boolean orderExist(long orderId)
    {
        HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap=orderSellRecords.getOrderSellHashMap();
        //try{
            if(orderSellHashMap.get(orderId)!=null){
                return true;
            }
        //}
        //catch(Exception e){
            return false;
        //}
    }

    //
    public void printOrdersHashMap(HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap ,boolean detailed)
    {
        //extract ArrayList from HashMap
        ArrayList<ArrayList<OrderSellProduct>> arrayListsFromHashMap=getArrayListsFromHashMap(orderSellHashMap);

        //how much orders
        int noOfOrders=arrayListsFromHashMap.size();

        if(detailed){

            //print extract for each basket
            for(ArrayList<OrderSellProduct> orderSellProduct : arrayListsFromHashMap){
                printBasket(orderSellProduct);
            }
        }
        System.out.println("number of orders: "+noOfOrders);
    }



    public boolean enoughGoodsInStok(int noOfWanted, SellProduct sellProduct)
    {
        int noOfGoodsInStok=sellProduct.getNoOfOneType();

        if(noOfGoodsInStok>=noOfWanted){
            return true;
        }
        else{
            return false;
        }
    }

    public long getOrderId() {
        return orderId;
    }

    public HashMap<Long, ArrayList<OrderSellProduct>> getOrderSellHashMapC(){
        HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap =orderSellRecords.getOrderSellHashMap();
        return orderSellHashMap;
    }

     public ArrayList<OrderSellProduct> cursellArrayListSellFromHashMap(HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap,long idOfOrder)
    {

        //getting Values for Iterate
        Iterator it = orderSellHashMap.keySet().iterator();

        while(it.hasNext()){

            //getting orderId from HashMap
            Long orderIdHere=(Long) it.next();

            if(idOfOrder==orderIdHere){

                //extract arraylist from hashmap
                ArrayList<OrderSellProduct> orderSellProductList=orderSellHashMap.get(orderIdHere);

                return orderSellProductList;
            }

        }

        return null;
    }

    public ArrayList<ArrayList<OrderSellProduct>> getArrayListsFromHashMap(HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap)
    {

        //getting Values for Iterate
        Iterator it = orderSellHashMap.keySet().iterator();

        //created arraylist hich will be contains ordered Baskets
        ArrayList<ArrayList<OrderSellProduct>> arrayListsFromHashMap=new ArrayList<ArrayList<OrderSellProduct>>();

        while(it.hasNext()){

            //getting orderId from HashMap
            Long orderIdHere=(Long) it.next();

            //extract arraylist from hashmap
            ArrayList<OrderSellProduct> orderSellProductList=orderSellHashMap.get(orderIdHere);

            //put extracted arraylist to arraylist which contains of arraList<orderSellpreoduct>
            arrayListsFromHashMap.add(orderSellProductList);
        }

        return arrayListsFromHashMap;
    }


    public double getBiggerDiscount(long customerId,long productId)
    {

        Customer customer=customerControl.findCustomer(customerId);
        SellProduct sellProduct=sellControl.findSellProduct(productId);

        //
        double customerDiscount=customer.getDiscount();
        double productDiscount=sellProduct.getDiscount();

        if(customerDiscount>productDiscount){
            return customerDiscount;
        }
        else{
            return productDiscount;
        }

    }
    

}
